# -*- coding: utf-8 -*-
# @Time    : 2024/5/30 12:30
# @Author  : Liaogan
# @FileName: 1111.py

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML

# Function to generate heart shape points
def heart_shape(t):
    x = 16 * np.sin(t)**3
    y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
    return x, y

# Initialize the figure and axis
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_facecolor('black')
ax.axis('equal')
ax.axis('off')
text = ax.text(0, 0, 'I love you', fontsize=24, fontweight='bold', ha='center', color='pink', visible=False)

# Number of particles in each firework
num_particles = 300
# Total frames
frames = 200

# Generate initial positions and velocities for fireworks
t = np.linspace(0, 2 * np.pi, num_particles)
x0, y0 = heart_shape(t)
angles = np.random.uniform(0, 2 * np.pi, num_particles)
speeds = np.random.uniform(1, 5, num_particles)
vx = speeds * np.cos(angles)
vy = speeds * np.sin(angles)
particles = (x0, y0, vx, vy)

# Firework launching parameters
launch_x = np.random.uniform(-5, 5, num_particles)
launch_y = np.random.uniform(-5, 5, num_particles)
launch_vy = np.random.uniform(20, 30, num_particles)
launch_vx = np.random.uniform(-1, 1, num_particles)

scatter = ax.scatter([], [], c=[], cmap='rainbow', alpha=0.6, s=2)

# Initialization function
def init():
    scatter.set_offsets(np.c_[[], []])
    scatter.set_array(np.array([]))
    text.set_visible(False)
    return scatter, text

# Animation function
def animate(frame):
    if frame < 50:  # Launch phase
        y_data = launch_y + launch_vy * frame / 10
        x_data = launch_x + launch_vx * frame / 10
    else:  # Explosion phase forming heart shape
        time = frame - 50
        x_data = x0 + vx * time / 10
        y_data = y0 + vy * time / 10
        text.set_visible(True)

    colors = np.random.rand(num_particles)
    scatter.set_offsets(np.c_[x_data, y_data])
    scatter.set_array(colors)
    return scatter, text

# Create the animation
ani = FuncAnimation(fig, animate, init_func=init, frames=frames, interval=50, blit=True)

# To display the animation inline in a Jupyter notebook
HTML(ani.to_jshtml())

# Save the animation as a GIF file (optional)
ani.save('firework_heart_with_text333.gif', writer='pillow')


